package com.xbongbong.pro.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormExplainEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.elasticsearch.util.XbbElasticsearchRestTemplate;
import com.xbongbong.paas.enums.BiRelTableEnum;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.EsAliasEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.clazz.ConditionClass;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.ConditionListHelp;
import com.xbongbong.paas.model.PaasFormExplainModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.pojo.dto.PrintLogDTO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsInitVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.redis.RedisTimeConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.config.ProBaseConfig;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.domain.entity.BiProWareEntity;
import com.xbongbong.pro.domain.entity.ChartEntity;
import com.xbongbong.pro.domain.entity.IndexSetEntity;
import com.xbongbong.pro.enums.BoardTypeEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.index.pojo.IndexSetDataPojo;
import com.xbongbong.pro.statistic.constant.ChartConstant;
import com.xbongbong.pro.statistic.enums.ChartCategoryTypeEnum;
import com.xbongbong.pro.statistic.enums.ChartPublishEnum;
import com.xbongbong.pro.statistic.enums.ChartTypeEnum;
import com.xbongbong.pro.statistic.enums.StatisticsTypeEnum;
import com.xbongbong.pro.statistic.help.CustomHelp;
import com.xbongbong.pro.statistic.model.BiProWareModel;
import com.xbongbong.pro.statistic.model.ChartCustomModel;
import com.xbongbong.pro.statistic.model.ChartModel;
import com.xbongbong.pro.statistic.pojo.SlaveSourcePojo;
import com.xbongbong.pro.statistic.pojo.dto.BatchPublishChartDTO;
import com.xbongbong.pro.statistic.pojo.dto.BiLimitDTO;
import com.xbongbong.pro.statistic.pojo.dto.ChartEditDTO;
import com.xbongbong.pro.statistic.pojo.dto.MappingInitDTO;
import com.xbongbong.pro.statistic.pojo.dto.ReInitDataDTO;
import com.xbongbong.pro.statistic.pojo.dto.SwitchDataDTO;
import com.xbongbong.pro.statistic.pojo.dto.TransChartDTO;
import com.xbongbong.pro.statistic.pojo.dto.UpdateChartSearchDTO;
import com.xbongbong.pro.statistic.service.BiProWareService;
import com.xbongbong.pro.statistic.service.ChartLimitService;
import com.xbongbong.pro.statistic.service.ChartScriptService;
import com.xbongbong.pro.statistic.service.DataWarehouseService;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.IndexSetModel;
import com.xbongbong.saas.service.toolbox.help.UpdatePackageRedisService;
import com.xbongbong.sys.model.CompanyModel;
import org.apache.http.HttpEntity;
import org.apache.http.nio.entity.NStringEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 梁鲁江
 * @version v1.0
 * @date 2020/5/26 14:31
 * @since v1.0
 */
@RestController
@RequestMapping(value = XbbProConstant.API_ROOT_PREFIX + "/chartScript")
public class ChartDataScript {
    private static final Logger LOG = LoggerFactory.getLogger(ChartDataScript.class);
    @Resource
    private DataWarehouseService dataWarehouseService;
    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private ProBaseConfig proBaseConfig;
    @Resource
    private ChartCustomModel chartCustomModel;
    @Resource
    private ChartModel chartModel;
    @Resource
    private CustomHelp customHelp;
    @Resource
    private BiProWareModel biProWareModel;
    @Resource
    private XbbElasticsearchRestTemplate xbbElasticsearchRestTemplate;
    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private UpdatePackageRedisService updatePackageRedisService;
    @Resource
    private PaasFormExplainModel paasFormExplainModel;
    @Resource
    private ThreadPoolTaskExecutor biInitThreadPool;
    @Resource
    private BiProWareService biProWareService;
    @Resource
    private ChartScriptService chartScriptService;
    @Resource
    private CompanyModel companyModel;
    @Resource
    private IndexSetModel indexSetModel;
    @Resource
    private ChartLimitService chartLimitService;

    @RequestMapping(value = "/setPrintLog", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String setPrintLog(@RequestBody @Valid PrintLogDTO printLogDTO, BindingResult br) throws Exception {
        XbbResponse<String> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if(!proBaseConfig.getXbbCode().equals(printLogDTO.getXbbCode())){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                xbbElasticsearchRestTemplate.setPrintLog(printLogDTO.getPrintLog());
                response = new XbbResponse<>(printLogDTO.getPrintLog().toString());
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 老表到新表的迁移
     * @param updateChartSearchDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/init/reChart", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String reChart(@Valid UpdateChartSearchDTO updateChartSearchDTO, BindingResult br) {
        XbbResponse<String> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // ----------------------------------repeat post----------------------------------------------
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(updateChartSearchDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(updateChartSearchDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            // ----------------------------------repeat post----------------------------------------------
            try {
                //验证码
                if(!proBaseConfig.getXbbCode().equals(updateChartSearchDTO.getXbbCode())){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                param.put("del",0);
                param.put("statisticsType",2);
                if(updateChartSearchDTO.getChartIdLTE() != null){
                    param.put("idLte",updateChartSearchDTO.getChartIdLTE());
                }
                if(updateChartSearchDTO.getChartIdGTE() != null){
                    param.put("idGte",updateChartSearchDTO.getChartIdGTE());
                }
                if(CollectionsUtil.isNotEmpty(updateChartSearchDTO.getCorpidList())) {
                    param.put("corpidIn", updateChartSearchDTO.getCorpidList());
                }
                if(CollectionsUtil.isNotEmpty(updateChartSearchDTO.getNotCorpidList())) {
                    param.put("corpidNotIn", updateChartSearchDTO.getNotCorpidList());
                }
                List<ChartEntity> list = chartCustomModel.findMutiEntitys(param);
                if(CollectionsUtil.isEmpty(list)){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, "数据查不到");
                }
                LOG.info("========开始处理==="+DateUtil.getString());
                Map<Long, PaasFormExplainEntity> formIdToExplainEntity = new HashMap<>(1 << 4);
                Set<Long> formIds = new HashSet<>();
                for (ChartEntity chartEntity : list) {
                    JSONObject diverSource = chartEntity.getDriverSources();
                    JSONArray slaveSources = chartEntity.getSlaveSources();
                    for (Object obj : slaveSources) {
                        JSONObject jsonObject = (JSONObject) obj;
                        Long formId = jsonObject.getLong("formId");
                        formIds.add(formId);
                    }
                    Long formId = diverSource.getLong("formId");
                    formIds.add(formId);
                }
                Map<String, Object> explainParam = new HashMap<>(1);
                explainParam.put("formIdIn", formIds);
                LOG.info(formIds.size()+ "========开始查询explain==="+DateUtil.getString());
                List<PaasFormExplainEntity> formExplainEntities = paasFormExplainModel.findEntitys(explainParam);
                formExplainEntities.forEach(e -> formIdToExplainEntity.put(e.getFormId(), e));
                LOG.info("========explain准备完毕==="+DateUtil.getString());
                Integer i = 0;
                for (ChartEntity chartEntity : list) {
                    try {
                        //linkAttrB    secondLinkAttr
                        //linkAttrBCopy    secondLinkAttrCopy
                        JSONObject driverSources = chartEntity.getDriverSources();
                        if(Objects.equals("-1", driverSources.getString("linkAttr"))){
                            String linkAttrCopy =  driverSources.getString("linkAttrCopy");
                            if(StringUtil.isNotEmpty(linkAttrCopy)){
                                driverSources.put("linkAttr",linkAttrCopy);
                            }
                        }
                        String secondLinkAttrCopy = driverSources.getString("secondLinkAttrCopy");
                        if(StringUtil.isNotEmpty(secondLinkAttrCopy)){
                            driverSources.put("linkAttrB",secondLinkAttrCopy);
                            driverSources.put("linkAttrBCopy",secondLinkAttrCopy);
                        }
                        JSONArray slaveSources = chartEntity.getSlaveSources();
                        if(CollectionsUtil.isNotEmpty(slaveSources)){
                            for (int j = 0; j < slaveSources.size(); j++) {
                                JSONObject slaveSource = slaveSources.getJSONObject(j);
                                if(Objects.equals("-1", slaveSource.getString("linkAttr"))){
                                    String linkAttrCopy =  slaveSource.getString("linkAttrCopy");
                                    if(StringUtil.isNotEmpty(linkAttrCopy)){
                                        slaveSource.put("linkAttr",linkAttrCopy);
                                    }
                                }
                            }
                        }
                        JSONArray displayArray = chartEntity.getDisplayAttr();
                        JSONArray rowGroupAttr = chartEntity.getRowGroupAttr();
                        JSONArray colGroupAttr = chartEntity.getColGroupAttr();
                        JSONArray summaryArray = chartEntity.getSummaryAttr();
                        JSONArray chartClassifyArray = chartEntity.getChartClassify();
                        JSONArray chartSeriesArray = chartEntity.getChartSeries();
                        JSONArray chartValueArray = chartEntity.getChartValue();
                        JSONArray penetrateArray = chartEntity.getPenetrateAttr();
                        JSONArray searchArray = chartEntity.getSearch();
                        JSONArray filterArray = chartEntity.getFilter();

                        handleLinkedAttr(displayArray, formIdToExplainEntity);
                        handleLinkedAttr(rowGroupAttr, formIdToExplainEntity);
                        handleLinkedAttr(colGroupAttr, formIdToExplainEntity);
                        handleLinkedAttr(summaryArray, formIdToExplainEntity);
                        handleLinkedAttr(chartClassifyArray, formIdToExplainEntity);
                        handleLinkedAttr(chartSeriesArray, formIdToExplainEntity);
                        handleLinkedAttr(chartValueArray, formIdToExplainEntity);
                        handleLinkedAttr(penetrateArray, formIdToExplainEntity);
                        handleLinkedAttr(searchArray, formIdToExplainEntity);
                        handleLinkedAttr(filterArray, formIdToExplainEntity);

                        customHelp.setSourcePojo(chartEntity);
                        chartCustomModel.update(chartEntity);
                        ChartEditDTO chartEditDTO = new ChartEditDTO();
                        chartEditDTO.setCorpid(chartEntity.getCorpid());
                        chartEditDTO.setChartId(chartEntity.getId());
                        LOG.info(i+"========发送数据前==="+DateUtil.getString());
                        dataWarehouseService.initData(chartEditDTO);
                        LOG.info(i+"========发送数据后==="+DateUtil.getString());
                    } catch (Exception e) {
                        LOG.error("error======"+chartEntity.getId(),e);
                    }
                    i++;
                    LOG.info("========total==="+ list.size()+"======="+i);
                }
                LOG.info("========结束处理==="+DateUtil.getString());
                response = new XbbResponse<>("");
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    @RequestMapping(value = "/init/reChartFinsh", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String reChartFinsh(@Valid UpdateChartSearchDTO updateChartSearchDTO, BindingResult br) {
        XbbResponse<String> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // ----------------------------------repeat post----------------------------------------------
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(updateChartSearchDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(updateChartSearchDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            // ----------------------------------repeat post----------------------------------------------
            try {
                //验证码
                if(!proBaseConfig.getXbbCode().equals(updateChartSearchDTO.getXbbCode())){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                LOG.info("=biInitThreadPool.getActiveCount()============"+biInitThreadPool.getActiveCount());
                response = new XbbResponse<>("");
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }



    @RequestMapping(value = "/init/reChartById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String reChartById(@RequestBody @Valid ReInitDataDTO reInitDataDTO, BindingResult br) {
        XbbResponse<String> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // ----------------------------------repeat post----------------------------------------------
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(reInitDataDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(reInitDataDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            // ----------------------------------repeat post----------------------------------------------
            try {
                //验证码
                if(!proBaseConfig.getXbbCode().equals(reInitDataDTO.getXbbCode())){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                param.put("id",reInitDataDTO.getChartId());
                List<ChartEntity> list = chartCustomModel.findEntitys(param);
                if(CollectionsUtil.isEmpty(list)){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, "数据查不到");
                }
                LOG.info("========开始处理==="+DateUtil.getString());
                Map<Long, PaasFormExplainEntity> formIdToExplainEntity = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                Set<Long> formIds = new HashSet<>();
                for (ChartEntity chartEntity : list) {
                    JSONObject diverSource = chartEntity.getDriverSources();
                    JSONArray slaveSources = chartEntity.getSlaveSources();
                    for (Object obj : slaveSources) {
                        JSONObject jsonObject = (JSONObject) obj;
                        Long formId = jsonObject.getLong("formId");
                        formIds.add(formId);
                    }
                    Long formId = diverSource.getLong("formId");
                    formIds.add(formId);
                }
                Map<String, Object> explainParam = new HashMap<>(1);
                explainParam.put("formIdIn", formIds);
                List<PaasFormExplainEntity> formExplainEntities = paasFormExplainModel.findEntitys(explainParam);
                formExplainEntities.forEach(e -> formIdToExplainEntity.put(e.getFormId(), e));
                Integer i = 0;
                for (ChartEntity chartEntity : list) {
                    try {
                        //linkAttrB    secondLinkAttr
                        //linkAttrBCopy    secondLinkAttrCopy
                        JSONObject driverSources = chartEntity.getDriverSources();
                        if(Objects.equals("-1", driverSources.getString("linkAttr"))){
                            String linkAttrCopy =  driverSources.getString("linkAttrCopy");
                            if(StringUtil.isNotEmpty(linkAttrCopy)){
                                driverSources.put("linkAttr",linkAttrCopy);
                            }
                        }
                        String secondLinkAttrCopy = driverSources.getString("secondLinkAttrCopy");
                        if(StringUtil.isNotEmpty(secondLinkAttrCopy)){
                            driverSources.put("linkAttrB",secondLinkAttrCopy);
                            driverSources.put("linkAttrBCopy",secondLinkAttrCopy);
                        }
                        JSONArray slaveSources = chartEntity.getSlaveSources();
                        if(CollectionsUtil.isNotEmpty(slaveSources)){
                            for (int j = 0; j < slaveSources.size(); j++) {
                                JSONObject slaveSource = slaveSources.getJSONObject(j);
                                if(Objects.equals("-1", slaveSource.getString("linkAttr"))){
                                    String linkAttrCopy =  slaveSource.getString("linkAttrCopy");
                                    if(StringUtil.isNotEmpty(linkAttrCopy)){
                                        slaveSource.put("linkAttr",linkAttrCopy);
                                    }
                                }
                            }
                        }
                        JSONArray displayArray = chartEntity.getDisplayAttr();
                        JSONArray rowGroupAttr = chartEntity.getRowGroupAttr();
                        JSONArray colGroupAttr = chartEntity.getColGroupAttr();
                        JSONArray summaryArray = chartEntity.getSummaryAttr();
                        JSONArray chartClassifyArray = chartEntity.getChartClassify();
                        JSONArray chartSeriesArray = chartEntity.getChartSeries();
                        JSONArray chartValueArray = chartEntity.getChartValue();
                        JSONArray penetrateArray = chartEntity.getPenetrateAttr();
                        JSONArray searchArray = chartEntity.getSearch();
                        JSONArray filterArray = chartEntity.getFilter();

                        handleLinkedAttr(displayArray, formIdToExplainEntity);
                        handleLinkedAttr(rowGroupAttr, formIdToExplainEntity);
                        handleLinkedAttr(colGroupAttr, formIdToExplainEntity);
                        handleLinkedAttr(summaryArray, formIdToExplainEntity);
                        handleLinkedAttr(chartClassifyArray, formIdToExplainEntity);
                        handleLinkedAttr(chartSeriesArray, formIdToExplainEntity);
                        handleLinkedAttr(chartValueArray, formIdToExplainEntity);
                        handleLinkedAttr(penetrateArray, formIdToExplainEntity);
                        handleLinkedAttr(searchArray, formIdToExplainEntity);
                        handleLinkedAttr(filterArray, formIdToExplainEntity);

                        customHelp.setSourcePojo(chartEntity);
                        chartCustomModel.update(chartEntity);
                        ChartEditDTO chartEditDTO = new ChartEditDTO();
                        chartEditDTO.setCorpid(chartEntity.getCorpid());
                        chartEditDTO.setChartId(chartEntity.getId());
                        dataWarehouseService.initData(chartEditDTO);
                    } catch (Exception e) {
                        LOG.error("error======"+chartEntity.getId(),e);
                    }
                    i++;
                    LOG.info("========total==="+ list.size()+"======="+i);
                }
                LOG.info("========结束处理==="+DateUtil.getString());
                response = new XbbResponse<>("");
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * 把is_correlation = 0 的置为del =1 ,加沉睡时间  当前时间-updateTime
     * @param transChartDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/init/deleteWareHouse", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String deleteWareHouse(@Valid TransChartDTO transChartDTO, BindingResult br) {
        XbbResponse<String> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // ----------------------------------repeat post----------------------------------------------
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(transChartDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(transChartDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            // ----------------------------------repeat post----------------------------------------------
            try {
                //验证码
                if(!proBaseConfig.getXbbCode().equals(transChartDTO.getXbbCode())){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                param.put("del",0);
                param.put("isCorrelation",0);
                long now = DateUtil.getNow();
                //一周期的废弃数据
                param.put("updateTimeLT",now-86400*7);
                //后面如果量大了要做分页
        /*        param.put("orderByStr","id");
                param.put("pageSize",PaasConstant.ES_MAX_PAGE_SIZE);*/
                List<BiProWareEntity> list = biProWareModel.findEntitys(param);
                if(CollectionsUtil.isEmpty(list)){
                    LOG.error("本期无数据需要删除"+ now);
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, "数据查不到");
                }
                Set<Long> idIn  =  new HashSet<>(list.size());
                for (BiProWareEntity biProWareEntity : list) {
                    Long id = biProWareEntity.getId();
                    idIn.add(id);
                    Integer relTable = biProWareEntity.getRelTable();
                    deleteByQuery(relTable,id,biProWareEntity.getCorpid());
                }
                //把删除的del置为1，同时更新updateTime
                biProWareModel.deleteAll(idIn);
                response = new XbbResponse<>("");
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    private void deleteByQuery(Integer relTable,Long warehouseId,String corpid) {
        HttpEntity httpEntity = null;
        try {
            //
            BiRelTableEnum  biRelTableEnum = BiRelTableEnum.getByCode(relTable);
            String index = biRelTableEnum.getIndex();
   /*         httpEntity = new NStringEntity("{\"query\":{\"bool\":{\"filter\":[{\"term\":{\"corpid.keyword\":\"" + corpid+ "\"}},{\"term\":{\"warehouseId\":" +
                    warehouseId +
                    "}}]}}}", ContentType.APPLICATION_JSON);
            xbbElasticsearchRestTemplate.getRestClient(index).performRequest("POST", index + "/" + biRelTableEnum.getType() + "/_delete_by_query", Collections.emptyMap(), httpEntity);
            */
            dataWarehouseService.deleteQueryByWareHouseId(warehouseId,index);

        } catch (Exception e) {
            LOG.error("",e);
        } finally {
            if (Objects.nonNull(httpEntity)) {
                try {
                    ((NStringEntity) httpEntity).close();
                } catch (Exception e) {
                    LOG.error("dataWarehouse.deleteByQuery",e);
                }
            }
        }
    }

    @RequestMapping(value = "/chartLimit", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartLimit( @Valid TransChartDTO transChartDTO, BindingResult br) {
        XbbResponse<String> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // ----------------------------------repeat post----------------------------------------------
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(transChartDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(transChartDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            // ----------------------------------repeat post----------------------------------------------
            try {
                //验证码
                if(!proBaseConfig.getXbbCode().equals(transChartDTO.getXbbCode())){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                Map<String, Object> chartMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                chartMap.put("columns","DISTINCT(corpid)");
                chartMap.put("del",0);
                chartMap.put("statisticsType",2);
                List<ChartEntity> list = chartCustomModel.findEntitys(chartMap);
                if(CollectionsUtil.isNotEmpty(list)){
                    for (ChartEntity chartEntity : list) {
                        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                        String corpid = chartEntity.getCorpid();
                        map.put("corpid",corpid);
                        map.put("columns","id, corpid ,saas_mark,business_type");
                        //TODO　　暂时不考虑ｅｎａｂｌｅ　，防止模板启用关闭造成的影响
                        //  map.put("enable",1);
                        map.put("del",0);
                        List<PaasFormEntityExt> formList = paasFormModel.list(map);
                        if(CollectionsUtil.isNotEmpty(formList)){
                            Map<String, Long> countMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                            for (PaasFormEntityExt paasFormEntityExt : formList) {
                                XbbRefTypeEnum xbbRefTypeEnum = null;
                                if(Objects.equals(SaasMarkEnum.SAAS.getCode(),paasFormEntityExt.getSaasMark())){
                                    xbbRefTypeEnum= XbbRefTypeEnum.getByCode(paasFormEntityExt.getBusinessType());
                                }else {
                                    xbbRefTypeEnum = XbbRefTypeEnum.PAAS;
                                }
                                updatePackageRedisService.getCountMap(countMap,corpid,paasFormEntityExt.getId(), xbbRefTypeEnum);
                            }
                            if(countMap !=null){
                                paasRedisHelper.setValue(RedisPrefixConstant.DATA_COUNT_FORM_ID, corpid,countMap, RedisTimeConstant.HUGE_DURATION);
                            }
                        }
                    }
                }
                response = new XbbResponse<>("");
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }



    private static void handleLinkedAttr(JSONArray jsonArray, Map<Long, PaasFormExplainEntity> map) throws XbbException{
        if (jsonArray == null) {
            return;
        }
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String originalAttr = jsonObject.getString("originalAttr");
            if (originalAttr != null && !Objects.equals("", originalAttr)) {
                continue;
            }
            Long formId = jsonObject.getLong("formId");
            PaasFormExplainEntity paasFormExplainEntity = map.get(formId);
            if (paasFormExplainEntity == null) {
                return;
            }
            Map<String, FieldAttrEntity> explain = ExplainUtil.getExplainMap(paasFormExplainEntity.getExplains());
            Integer fieldType = jsonObject.getInteger("fieldType");
            if (FieldTypeEnum.LINK_BUSINESS_SINGLE.getType().equals(fieldType) || FieldTypeEnum.LINK_BUSINESS_MULTI.getType().equals(fieldType) || FieldTypeEnum.BIRTHDAY.getType().equals(fieldType)) {
                if (jsonObject.getString("linkedText") != null) {
                    String linkedText = jsonObject.getString("linkedText");
                    FieldAttrEntity fieldAttrEntity = explain.get(linkedText);
                    JSONObject linkedAttr = JSON.parseObject(JSON.toJSONString(fieldAttrEntity));
                    linkedAttr.put("formId", formId);
                    jsonObject.put("linkedAttr",linkedAttr);
                    jsonArray.set(i, jsonObject);
                }
            }
        }
    }

    @RequestMapping(value = "/chartSearch", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String chartSearch(@Valid UpdateChartSearchDTO updateChartSearchDTO, BindingResult br) {
        XbbResponse<String> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // ----------------------------------repeat post----------------------------------------------
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(updateChartSearchDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(updateChartSearchDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            // ----------------------------------repeat post----------------------------------------------
            try {
                //验证码
                if(!proBaseConfig.getXbbCode().equals(updateChartSearchDTO.getXbbCode())){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                Map<String, Object> chartMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                //拉出来updateTime，防止被赋默认值
                chartMap.put("columns","id,corpid,update_time,search");
                chartMap.put("del",0);
                chartMap.put("statisticsType",2);
                if(updateChartSearchDTO.getChartIdLTE() != null){
                    chartMap.put("idLte",updateChartSearchDTO.getChartIdLTE());
                }
                if(updateChartSearchDTO.getChartIdGTE() != null){
                    chartMap.put("idGte",updateChartSearchDTO.getChartIdGTE());
                }
                if(CollectionsUtil.isNotEmpty(updateChartSearchDTO.getCorpidList())) {
                    chartMap.put("corpidIn", updateChartSearchDTO.getCorpidList());
                }
                if(CollectionsUtil.isNotEmpty(updateChartSearchDTO.getNotCorpidList())) {
                    chartMap.put("corpidNotIn", updateChartSearchDTO.getNotCorpidList());
                }
                List<ChartEntity> list = chartCustomModel.findEntitys(chartMap);
                if(CollectionsUtil.isNotEmpty(list)){
                    for (ChartEntity chartEntity : list) {
                        JSONArray search = chartEntity.getSearch();
                        if(CollectionsUtil.isEmpty(search)){
                            continue;
                        }
                        Iterator<Object> iterator = search.iterator();
                        while (iterator.hasNext()){
                            JSONObject json = (JSONObject) iterator.next();
                            //移除Memo
                            if(Objects.equals(json.getInteger("fieldType"),FieldTypeEnum.MEMO.getType())){
                                iterator.remove();
                                continue;
                            }
                            try {
                                //当again=1或者（当symbol为空且fieldType不为空）
                                if( Objects.equals(updateChartSearchDTO.getAgain(),1)  || ( json.get("symbol") ==null && json.getInteger("fieldType")!=null )){
                                    FieldTypeEnum fieldTypeEnum = FieldTypeEnum.getFieldTypeEnum(json.getInteger("fieldType"));
                                    List<ConditionClass> conditionList = null;
                                    try {
                                        conditionList = ConditionListHelp.getConditionList(fieldTypeEnum,1);
                                    } catch (XbbException e) {
                                        //有些类型不筛选
                                        continue;
                                    }
                                    if(CollectionsUtil.isNotEmpty(conditionList)){
                                        if(fieldTypeEnum!=null){
                                            if(Objects.equals(EsAliasEnum.DATE.getEsAlias(),fieldTypeEnum.getEsalias())){
                                                json.put("symbol",ConditionEnum.RANGE.getSymbol());
                                            }else if(Objects.equals(FieldTypeEnum.TEXTAREA.getType(),fieldTypeEnum.getType())
                                                    || Objects.equals(FieldTypeEnum.SPECIFICATION.getType(),fieldTypeEnum.getType())
                                                    || Objects.equals(FieldTypeEnum.RICH_TEXT_EDITOR.getType(),fieldTypeEnum.getType())  ){
                                                json.put("symbol", ConditionEnum.LIKE.getSymbol());
                                            }else if(Objects.equals(FieldTypeEnum.LINK_BUSINESS_MULTI.getType(),fieldTypeEnum.getType())){
                                                json.put("symbol", ConditionEnum.IN.getSymbol());
                                            }else if(Objects.equals(FieldTypeEnum.SELECT_PRODUCT.getType(),fieldTypeEnum.getType())
                                                    || Objects.equals(FieldTypeEnum.CREATOR_DEPT.getType(),fieldTypeEnum.getType())){
                                                json.put("symbol", ConditionEnum.INCLUDE.getSymbol());
                                            } else {
                                                json.put("symbol", ConditionEnum.EQUAL.getSymbol());
                                            }
                                        }else {
                                            json.put("symbol", ConditionEnum.EQUAL.getSymbol());
                                        }
                                        json.put("whereList",conditionList);
                                    }
                                }
                                if(json.getJSONObject("parentSubFormAttr") !=null && json.getJSONObject("parentSubFormAttr").get("businessType") != null && json.get("businessType") ==null ){
                                    json.put("businessType", json.getJSONObject("parentSubFormAttr").get("businessType"));
                                }
                            } catch (Exception e) {
                                LOG.error("",e);
                            }
                        }
                        chartCustomModel.update(chartEntity);
                        LOG.info("========="+chartEntity.getId());
                    }
                }
                response = new XbbResponse<>("");
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/init/labelChart", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String labelChart(@Valid UpdateChartSearchDTO updateChartSearchDTO, BindingResult br) {
        XbbResponse<String> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // ----------------------------------repeat post----------------------------------------------
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(updateChartSearchDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(updateChartSearchDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            // ----------------------------------repeat post----------------------------------------------
            try {
                //验证码
                if(!proBaseConfig.getXbbCode().equals(updateChartSearchDTO.getXbbCode())){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                param.put("del",0);
                param.put("statisticsType",2);
                List<ChartEntity> list = chartCustomModel.findEntitys(param);
                if(CollectionsUtil.isEmpty(list)){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, "数据查不到");
                }
                LOG.info("========开始处理==="+DateUtil.getString());
                Integer i = 0;
                for (ChartEntity chartEntity : list) {
                    try {
                        JSONArray displayArray = chartEntity.getDisplayAttr();
                        JSONArray rowGroupAttr = chartEntity.getRowGroupAttr();
                        JSONArray colGroupAttr = chartEntity.getColGroupAttr();
                        JSONArray summaryArray = chartEntity.getSummaryAttr();
                        JSONArray chartClassifyArray = chartEntity.getChartClassify();
                        JSONArray chartSeriesArray = chartEntity.getChartSeries();
                        JSONArray chartValueArray = chartEntity.getChartValue();
                        JSONArray penetrateArray = chartEntity.getPenetrateAttr();
                        JSONArray searchArray = chartEntity.getSearch();
                        JSONArray filterArray = chartEntity.getFilter();
                        if (handleAttrLabel(displayArray) | handleAttrLabel(rowGroupAttr) | handleAttrLabel(colGroupAttr)
                                | handleAttrLabel(summaryArray) | handleAttrLabel(chartClassifyArray) | handleAttrLabel(chartSeriesArray)
                                | handleAttrLabel(chartValueArray) | handleAttrLabel(penetrateArray) | handleAttrLabel(searchArray)
                                | handleAttrLabel(filterArray)) {
                            chartCustomModel.update(chartEntity);
                        }
                    } catch (Exception e) {
                        LOG.error("error======"+chartEntity.getId(),e);
                    }
                    i++;
                    LOG.info("========total==="+ list.size()+"======="+i);
                }
                LOG.info("========结束处理==="+DateUtil.getString());
                response = new XbbResponse<>("");
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/init/delAddress", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delAddress(@Valid UpdateChartSearchDTO updateChartSearchDTO, BindingResult br) {
        XbbResponse<String> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // ----------------------------------repeat post----------------------------------------------
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(updateChartSearchDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(updateChartSearchDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            // ----------------------------------repeat post----------------------------------------------
            try {
                //验证码
                if(!proBaseConfig.getXbbCode().equals(updateChartSearchDTO.getXbbCode())){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                param.put("del",0);
                param.put("statisticsType",2);
                List<ChartEntity> list = chartModel.findEntitys(param);
                if(CollectionsUtil.isEmpty(list)){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, "数据查不到");
                }
                LOG.info("========开始处理==="+DateUtil.getString());
                Integer i = 0;
                for (ChartEntity chartEntity : list) {
                    try {
                        JSONArray displayArray = chartEntity.getDisplayAttr();
                        JSONArray penetrateArray = chartEntity.getPenetrateAttr();
                        JSONArray searchArray = chartEntity.getSearch();
                        JSONArray filterArray = chartEntity.getFilter();
                        if (delAddressAttr(displayArray) | delAddressAttr(penetrateArray) | delAddressAttr(searchArray)
                                | delAddressAttr(filterArray)) {
                            chartModel.update(chartEntity);
                        }
                    } catch (Exception e) {
                        LOG.error("error======"+chartEntity.getId(),e);
                    }
                    i++;
                    LOG.info("========total==="+ list.size()+"======="+i);
                }
                LOG.info("========结束处理==="+DateUtil.getString());
                response = new XbbResponse<>("");
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    private static boolean handleAttrLabel(JSONArray jsonArray) {
        boolean flag = false;
        if (jsonArray != null) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                if (jsonObject.get("label") == null || Objects.equals(jsonObject.get("label"), "")) {
                    jsonObject.put("label", jsonObject.get("labelName"));
                    flag = true;
                }
            }
        }
        return flag;
    }

    private static boolean delAddressAttr(JSONArray jsonArray) {
        boolean flag = false;
        if (jsonArray != null) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Integer fieldType = jsonObject.getInteger("fieldType");
                if (Objects.equals(fieldType, FieldTypeEnum.ADDRESS.getType())) {
                    jsonArray.remove(jsonObject);
                    flag = true;
                }
            }
        }
        return flag;
    }

    @RequestMapping(value = "/getSerAndWarId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getSerAndWareId(@RequestBody @Valid ReInitDataDTO reInitDataDTO, BindingResult br) {
        XbbResponse<JSONObject> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // ----------------------------------repeat post----------------------------------------------
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(reInitDataDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(reInitDataDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            // ----------------------------------repeat post----------------------------------------------
            try {
                //验证码
                if(!proBaseConfig.getXbbCode().equals(reInitDataDTO.getXbbCode())){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
                param.put("id",reInitDataDTO.getChartId());
                List<ChartEntity> list = chartCustomModel.findEntitys(param);
                if(CollectionsUtil.isEmpty(list)){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, "数据查不到");
                }
                ChartEntity chartEntity = list.get(0);
                SlaveSourcePojo driverSourcePojo = chartEntity.getDriverSources().toJavaObject(SlaveSourcePojo.class);
                BiRelTableEnum biRelTableEnum = customHelp.getBiRelTableEnum(driverSourcePojo.getBusinessType(), driverSourcePojo.getSaasMark());
                String  tableSerial = biProWareService.getTableSerial(chartEntity);
                List<BiProWareEntity> wareList = biProWareService.getBiProWareEntities(chartEntity.getCorpid(),tableSerial);
                JSONObject json = new JSONObject();
                json.put("biRelTableEnum",biRelTableEnum.getIndex());
                json.put("tableSerial",tableSerial);
                json.put("wareId",wareList.get(0).getId());
                LOG.error("============"+json.toJSONString());
                response = new XbbResponse<>(json);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
    @RequestMapping(value = "/addAllAlias", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public String addAllAlias(@RequestBody @Valid MappingInitDTO mappingInitDTO, BindingResult br) throws Exception {
        XbbResponse<PaasFormDataEsInitVO> response;

        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if(!proBaseConfig.getXbbCode().equals(mappingInitDTO.getXbbCode())){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                PaasFormDataEsInitVO paasFormDataEsInitVO = chartScriptService.addAllAlias(mappingInitDTO);
                response = new XbbResponse<>(paasFormDataEsInitVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/biProWareId/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public String biProWareIdGet(@RequestBody @Valid ReInitDataDTO reInitDataDTO, BindingResult br) throws Exception {
        XbbResponse<Long> response;

        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if(!proBaseConfig.getXbbCode().equals(reInitDataDTO.getXbbCode())){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                ChartEntity chartEntity = chartModel.getByKey(reInitDataDTO.getChartId(), reInitDataDTO.getCorpid());
                Long wareId = null;
                if (Objects.nonNull(chartEntity)) {
                    List<BiProWareEntity> list = biProWareService.getBiProWareEntities(chartEntity);
                    if (CollectionsUtil.isNotEmpty(list)) {
                        wareId = list.get(0).getId();
                    }
                }
                response = new XbbResponse<>(wareId);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }


    /**
     * 处理副指标图批量撤销的问题
     * @param batchPublishChartDTO
     * @param br
     * @return java.lang.String
     * @throws Exception
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/batchCancelPublish", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public String batchPublishChart(@RequestBody @Valid BatchPublishChartDTO batchPublishChartDTO, BindingResult br) throws Exception {
        XbbResponse<JSONObject> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            if (!proBaseConfig.getXbbCode().equals(batchPublishChartDTO.getXbbCode())) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006);
            }

            // 获取所有公司
            List<String> corpidList = batchPublishChartDTO.getCorpidList();
            if (null == corpidList){
                corpidList = companyModel.getCorpidList();
            }
            corpidList = (null != corpidList) ? corpidList : new ArrayList<>();

            JSONObject resultJson = new JSONObject();

            LOG.warn("batchPublishChart begin");
            LOG.warn(corpidList.size() + "家公司");

            Integer pageSize = 20;


            LOG.info("开始刷脚本："+corpidList.size());
            List<ChartEntity> updateChartList = new ArrayList<>();
            List<IndexSetEntity> updateIndexSetList = new ArrayList<>();
            corpidList.forEach(corpid -> {
                batchCancelPublish(corpid, updateIndexSetList, updateChartList);
            });
            // 批量更新图表
            Integer chartSize = updateChartList.size();
            for (int i = 1; i <= (updateChartList.size() / pageSize) + 1; i++) {
                Integer start = (i - 1) * pageSize;
                Integer end = i * pageSize;
                end = end > chartSize ? chartSize : end;
                List<ChartEntity> subList = updateChartList.subList(start,end);
                if (CollectionsUtil.isEmpty(subList)){
                    continue;
                }
                chartCustomModel.updateBatchNoCorpId(subList);
            }
            // 批量更新首页配置
            Integer indexSetSize = updateIndexSetList.size();
            for (int i = 1; i <= (indexSetSize / pageSize) + 1; i++) {
                Integer start = (i - 1) * pageSize;
                Integer end = i * pageSize;
                end = end > indexSetSize ? indexSetSize : end;
                List<IndexSetEntity> subList = updateIndexSetList.subList(start,end);
                if (CollectionsUtil.isEmpty(subList)){
                    continue;
                }
                indexSetModel.updateBatchNoCorpId(subList);
            }
            LOG.warn("batchPublishChart over");

            response = new XbbResponse<>(resultJson);
        }
        return JSON.toJSONString(response);
    }
    private void batchCancelPublish(String corpid, List<IndexSetEntity> updateIndexSetList, List<ChartEntity> updateChartList){
        //查找发布状态的副指标图表
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("chartType", ChartTypeEnum.SECOND_TARGET_CHART.getCode());
        param.put("publish", ChartPublishEnum.PUBLISH.getCode());
        param.put("del", DelEnum.NORMAL.getDel());
        List<ChartEntity> chartList = chartCustomModel.findEntitys(param);
        if (CollectionsUtil.isEmpty(chartList)){
            return;
        }
        List<Long> chartIds = chartList.stream().map(ChartEntity::getId).collect(Collectors.toList());
        chartList.stream().forEach(chart ->chart.setPublish(ChartPublishEnum.NOT_PUBLISH.getCode()));
        updateChartList.addAll(chartList);

        //查找首页默认角色的设置
        List<IndexSetEntity> indexSetList = indexSetModel.getCompanyConfig(corpid);

        for (IndexSetEntity indexSet : indexSetList) {
            Integer boardType = indexSet.getBoardType();
            //跳过网页右侧分类,以及移动端专注模式下的右侧（工作tab）
            if (Objects.equals(ChartCategoryTypeEnum.WEB_INDEX_RIGHT.getCode(), boardType) || Objects.equals(BoardTypeEnum.FOCUS_DINGTALK_WORK.getCode(), boardType)) {
                continue;
            }
            JSONArray contentData = indexSet.getContentData();
            List<IndexSetDataPojo> indexSetDataPojos = contentData.toJavaList(IndexSetDataPojo.class);
            //遍历获取（在首页）需要删除的图
            List<IndexSetDataPojo> deletePojoList = new ArrayList<>();
            List<Integer> sortList = new ArrayList<>();
            for (IndexSetDataPojo pojo : indexSetDataPojos) {
                //根据chartId和type确认是当前图表
                Long indexChartId = pojo.getChartCategoryId();
                Integer type = pojo.getChartCategoryType();
                Integer statisticsType = pojo.getStatisticsType();
                if (indexChartId == null || type == null) {
                    continue;
                }
                if (chartIds.contains(indexChartId) && Objects.equals(type, ChartConstant.CHART_CATEGORY_TYPE_CHART) && Objects.equals(statisticsType, StatisticsTypeEnum.CUSTOM_STATISTIC.getCode())) {
                    deletePojoList.add(pojo);
                    sortList.add(pojo.getSort());
                    if (deletePojoList.size() >= chartIds.size()){
                        break;
                    }
                }
            }
            if (CollectionsUtil.isEmpty(deletePojoList)) {
                continue;
            }
            //移除需要删除的图
            indexSetDataPojos.removeAll(deletePojoList);
            Collections.sort(sortList);

            //遍历首页的图，将（sort大于被删除图sort）sort值减一
            for (IndexSetDataPojo pojo : indexSetDataPojos) {
                Integer sort = pojo.getSort();
                for (Integer item : sortList) {
                    if (sort > item) {
                        sort = sort - BasicConstant.ONE;
                        pojo.setSort(sort);
                    }else {
                        break;
                    }
                }
            }
            contentData = JSONArray.parseArray(JSON.toJSONString(indexSetDataPojos));
            indexSet.setUpdateTime(DateTimeUtil.getInt());
            indexSet.setContentData(contentData);
            updateIndexSetList.add(indexSet);
        }
    }

    @RequestMapping(value = "/switchCK", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public String switchCK(@RequestBody @Valid SwitchDataDTO mappingInitDTO, BindingResult br) throws Exception {
        XbbResponse<String> response;

        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if(!proBaseConfig.getXbbCode().equals(mappingInitDTO.getXbbCode())){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                chartLimitService.switchCK(mappingInitDTO.getCorpid(),mappingInitDTO.getFlag());
                response = new XbbResponse<>(mappingInitDTO.getCorpid()+"_"+mappingInitDTO.getFlag());
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    
    @RequestMapping(value = "/setChartLimt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public String setChartLimt(@RequestBody @Valid SwitchDataDTO switchDataDTO, BindingResult br) throws Exception {
        XbbResponse<String> response;

        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if(!proBaseConfig.getXbbCode().equals(switchDataDTO.getXbbCode())){
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                chartLimitService.setChartLimtFlag(switchDataDTO.getCorpid(),switchDataDTO.getFlag(),switchDataDTO.getSize());
                response = new XbbResponse<>(switchDataDTO.getCorpid()+"_"+switchDataDTO.getFlag());
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 基于 ChartLimitTypeEnum 设置bi图表相关数量限制
     * @param biLimitDTO
     * @param br
     * @return java.lang.String
     * @throws Exception
     * @author zcp
     * @since v1.0
     * @version v1.0
     */
    @RequestMapping(value = "/setBiChartLimt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public String setBiChartLimt(@RequestBody @Valid BiLimitDTO biLimitDTO, BindingResult br) throws Exception {
        XbbResponse<String> response;

        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                if (!proBaseConfig.getXbbCode().equals(biLimitDTO.getXbbCode())) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                chartLimitService.setBiChartLimt(biLimitDTO);
                response = new XbbResponse<>(biLimitDTO.getCorpid() + "_" + biLimitDTO.getCode());
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 修复多表双轴图没有originalAttr，全量刷，不限制公司
     *
     * @param transChartDTO
     * @param br
     * @return java.lang.String
     * @throws XbbException
     * @author xinpeng.jiang
     * @date 2022/2/23 15:44
     * @version 1.0
     */
    @RequestMapping(value = "/repair/originalAttr", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String repairOriginalAttr(@RequestBody @Valid TransChartDTO transChartDTO, BindingResult br) {
        XbbResponse<String> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            // ----------------------------------repeat post----------------------------------------------
            // 判断是否跳过重复提交校验
            if (!Boolean.TRUE.equals(transChartDTO.getIgnoreConCheck())) {
                //重复提交校验
                if (!paasRedisHelper.checkConcurrentLock(transChartDTO)) {
                    response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                    return JSON.toJSONString(response);
                }
            }
            // ----------------------------------repeat post----------------------------------------------
            try {
                //验证码
                if (!proBaseConfig.getXbbCode().equals(transChartDTO.getXbbCode())) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100006, SystemErrorCodeEnum.API_ERROR_100006.getMsg());
                }
                chartScriptService.repairOriginalAttr(transChartDTO);
                response = new XbbResponse<>("操作成功");
                LOG.warn("操作成功");
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
